package com.qdh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qdh.model.Member;
import com.qdh.model.Orders;
import com.qdh.model.Product;
import com.qdh.model.Traveller;
import com.qdh.service.MemberService;
import com.qdh.service.OrdersService;
import com.qdh.service.ProductService;
import com.qdh.service.TravellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    @Autowired
    ProductService productService;
    @Autowired
    TravellerService travellerService;
    @Autowired
    MemberService memberService;
    //order/findAll?page=1&size=5
    @RequestMapping("findAll")
    public String findAll(@RequestParam(name = "page",defaultValue = "1") int page,
                          @RequestParam(name = "size",defaultValue = "5") int size,
                          Model model) {
        PageHelper.startPage(page, size);
        List<Orders> list = ordersService.findAll();
        PageInfo<Orders> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "order-list";
    }
    //order/add
    @RequestMapping("add")
    public String add(Model model) {
        List<Product> productList = productService.findAll();
        model.addAttribute("productList", productList);
        List<Traveller> travellerList = travellerService.findAll();
        model.addAttribute("travellerList", travellerList);
        List<Member> memberList = memberService.findAll();
        model.addAttribute("memberList", memberList);
        return "order-add";
    }
    //order/save
    @RequestMapping("save")
    public String save(Orders orders) {
        ordersService.insert(orders);
        return "redirect:/order/findAll";
    }
    //order/findById?id=37
    @RequestMapping("findById")
    public String findById(Integer id, Model model) {
        Orders order = ordersService.findById(id);
        model.addAttribute("order", order);
        return "order-show";
    }
    //order/editById?id=38
    @RequestMapping("editById")
    public String editById(Integer id, Model model) {
        Orders order = ordersService.findById(id);
        model.addAttribute("order", order);
        List<Product> productList = productService.findAll();
        model.addAttribute("productList", productList);
        return "order-edit";
    }
    //order/edit
    @RequestMapping("edit")
    public String edit(Orders order) {
        ordersService.updateOrders(order);
        return "redirect:/order/findAll";
    }
    //order/deleteById?id=1
    @RequestMapping("deleteById")
    public String deleteById(Integer id) {
        ordersService.deleteOrders(id);
        return "redirect:/order/findAll";
    }
}
